package com.ymatou.iapi.trading.userquery.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.CollectionUtils;

import com.ymatou.iapi.trading.ordersync.service.SyncVerifyService;
import com.ymatou.iapi.trading.userquery.parameter.GetBuyerFirstOrderTimeRequestBean;
import com.ymatou.iapi.trading.userquery.parameter.resp.GetBuyerFirstOrderTimeResp;
import com.ymatou.iapi.trading.userquery.service.GetBuyerFirstOrderTimeRequestCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.model.mongo.query.UserIndex;
import com.ymttest.database.mongo.UserIndexQueryWapper;
import com.ymttest.database.mongo.config.UserIndexField;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 获取买家第一笔订单
 */
@SuppressWarnings("rawtypes")
@TestSuite
public class Ts_GetBuyerFirstOrderTimeRequest {

	private static GetBuyerFirstOrderTimeRequestBean req;
	private static GetBuyerFirstOrderTimeRequestCall call;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile(" 获取买家第一笔订单");
	}

	@Before
	public void caseUp() {
		req = new GetBuyerFirstOrderTimeRequestBean();
		call = new GetBuyerFirstOrderTimeRequestCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private void verify(){
		GetBuyerFirstOrderTimeResp expect  = new GetBuyerFirstOrderTimeResp();
		constructExpectedResult(expect);
		GetBuyerFirstOrderTimeResp actual = call.getBuyerFirstOrderTimeResp();
		DataUtil.verify(expect.getCreatedTime(), actual.getCreatedTime(), "验证CreatedTime");
	}

	private void constructExpectedResult(GetBuyerFirstOrderTimeResp resp) {
		Query query = new Query(Criteria.where(UserIndexField.USER_ID).is(req.getBuyerId()));
		query.fields().include(UserIndexField.ADD_TIME);
		query.with(new Sort(Sort.Direction.ASC, UserIndexField.ADD_TIME));
		query.limit(1);
		List<UserIndex> userIndiceList = new UserIndexQueryWapper().find(query, req.getBuyerId(), UserIndex.class);
		if (!CollectionUtils.isEmpty(userIndiceList)) {
			resp.setCreatedTime(userIndiceList.get(0).getAddTime());
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetBuyerFirstOrderTimeRequest_001() {
		Logger.start(true, "根据用户id查询用户第一笔订单");
		try {

			OrderCallService orderCallService = new OrderCallService();

			Map user = orderCallService.registerUser();
			Map orderMap = orderCallService.createM2cOrder(user);

			int orderId = (Integer) orderMap.get("orderId");
			int userId = (Integer) user.get("userid");

			SyncVerifyService.getSyncResult(orderId);

			req.setBuyerId(userId);
			call.setData(req);
			call.callService();
			DataUtil.verify(ECode.SUCCESS,call.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetBuyerFirstOrderTimeRequest_002() {
		Logger.start(true, "userId 不存在");
		try {
			int userId = 123456;
			req.setBuyerId(userId);
			call.setData(req);
			call.callService();
			DataUtil.verify(ECode.SUCCESS,call.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetBuyerFirstOrderTimeRequest_003() {
		Logger.start(true, "userId 为null");
		try {
			int userId = 123456;
			req.setBuyerId(null);
			call.setData(req);
			call.callService();
			DataUtil.verify(ECode.FAILED400,call.getCode(), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}
